﻿using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;

namespace LIK.NetworkTools
{
    public class NicEnumerator : List<NetworkInterfaceInfo>
    {
        public NetworkInterfaceInfo SelectedNic { get; set; }

        public static NicEnumerator Instance;

        static NicEnumerator()
        {
            if (Instance == null)
            {
                Instance = new NicEnumerator();
            }
        }

        private NicEnumerator()
        {
            this.ReloadNetworkInterfaces();
        }

        public void ReloadNetworkInterfaces()
        {
            this.Clear();
            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.OperationalStatus == OperationalStatus.Up &&
                    (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet 
                    || nic.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet
                    || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211))
                {
                    if (nic.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
                        nic.NetworkInterfaceType == NetworkInterfaceType.Tunnel)
                    {
                        continue;
                    }
                    var nInterface = new NetworkInterfaceInfo
                    {
                        Title = nic.Name,
                        Id = nic.Id,
                        Description = nic.Description,
                        MacAddress = nic.GetPhysicalAddress().GetAddressBytes().ToReadable()
                    };
                    var ipProps = nic.GetIPProperties();
                    foreach (var ipAddres in ipProps.UnicastAddresses)
                    {
                        if (!ipAddres.Address.IsIPv6LinkLocal)
                        // && ipAddres.Address.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6)
                        {
                            var strIpAddress = ipAddres.Address.ToString();
                            if (!strIpAddress.Contains("."))
                            {
                                continue;
                            }
                            //PhysicalAddress mac = nic.GetPhysicalAddress();
                            //nInterface.MacAddress = mac.ToString();
                            nInterface.Ip = strIpAddress;
                            nInterface.Mask = ipAddres.IPv4Mask.ToString();
                            break;
                        }
                    }
                    this.Add(nInterface);
                }
            }
        }

        public static void SetDefaultNic(string selectedNicMac)
        {
            if (!string.IsNullOrEmpty(selectedNicMac))
            {
                foreach (var nic in NicEnumerator.Instance)
                {
                    if (nic.MacAddress.Equals(selectedNicMac, StringComparison.OrdinalIgnoreCase))
                    {
                        NicEnumerator.Instance.SelectedNic = nic;
                        return;
                    }
                }
            }
            else if (NicEnumerator.Instance.Count > 0)
            {
                NicEnumerator.Instance.SelectedNic = NicEnumerator.Instance[0];
            }
        }
    }

}
